﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public enum gamestate
    {
        DEPLACEMENT, DEFAUT, ATTAQUE, ANIMATION
    }
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D image;
        Carte map;
        hero victime;
        hero perso_courant;
        gamestate gs;
  public static      SoundEffect soundEffect;
  public static SoundEffect soundEffect2;
  public static SoundEffect soundEffect3;
  public static SoundEffect soundEffect4;
        Texture2D nuite;
        Texture2D parchemint2;
        Texture2D parchemint3;
        Texture2D parchemint4;
        Texture2D parchemint5;
        Texture2D parchemint6;
        Texture2D parchemint7;
        Texture2D parchemint8;
        Texture2D parchemint9;
        Texture2D parchemint10;
        Texture2D parchemint11;
        Texture2D parchemint12;
        Texture2D parchemint13;
        Texture2D parchemint14;
        Texture2D parchemint15;
        Texture2D parcheminTexture;
        Vector2 nuitepos;
        Vector2 parcheminPosition;
        public static int menu;
        Texture2D curseurText;
        Vector2 curseurp;
        public static bool ssm;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            menu = 0;
            curseurp = new Vector2(10, 20);
          
            nuitepos = new Vector2(0, 0);

            parcheminPosition = new Vector2(0, graphics.PreferredBackBufferHeight / 3);
            perso_courant = new hero(Content.Load<Texture2D>("sprite1_p1_1"), 70, 5);

            //this.graphics.ToggleFullScreen();
            base.Initialize();
            gs = gamestate.DEFAUT;
        }

        /// <summary>
        /// Loadntent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
       
            spriteBatch = new SpriteBatch(GraphicsDevice);
            nuite = Content.Load<Texture2D>("ne");
            parcheminTexture = Content.Load<Texture2D>("parchemin11");
            parchemint2 = Content.Load<Texture2D>("parchemin113");
            parchemint3 = Content.Load<Texture2D>("parcheminst1");
            parchemint4 = Content.Load<Texture2D>("parcheminst2");
            parchemint5 = Content.Load<Texture2D>("parcheminst3");
            parchemint6 = Content.Load<Texture2D>("parcheminst4");
            parchemint7 = Content.Load<Texture2D>("parcheminst5");
            parchemint8 = Content.Load<Texture2D>("parcheminst6");
            parchemint9 = Content.Load<Texture2D>("parcheminst7");

            parchemint10 = Content.Load<Texture2D>("parcheminst8");
            parchemint11 = Content.Load<Texture2D>("parcheminst9");
            parchemint12 = Content.Load<Texture2D>("parcheminst10");
            parchemint13 = Content.Load<Texture2D>("parcheminst11");
            parchemint14 = Content.Load<Texture2D>("parcheminst12");
            parchemint15 = Content.Load<Texture2D>("parcheminst13");
            soundEffect = Content.Load<SoundEffect>("epee");
            soundEffect2 = Content.Load<SoundEffect>("monstre");
            soundEffect3 = Content.Load<SoundEffect>("Victoire");
            soundEffect4 = Content.Load<SoundEffect>("silence");
            image = Content.Load<Texture2D>("losange_herbe");
            map = new Carte(10, 10, image);
            curseurText = Content.Load<Texture2D>("cu");
            victime = new hero(Content.Load<Texture2D>("Sprite2_p1"), 70, 0);
            map.acces[2, 2].changer_perso(victime);
            map.acces[2, 2].contient.pos = new Coordonées(2, 2);
            map.acces[7, 7].changer_perso(perso_courant);
            map.acces[7, 7].contient.pos = new Coordonées(7, 7);


            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
      
            // TODO: Add your update logic here
            //map.est_allumee();
            switch (gs)
            {
                case (gamestate.DEPLACEMENT): gs = fonctions.déplacement_menu(perso_courant, map); break;
                case (gamestate.ATTAQUE): gs = fonctions.attaque_menu(perso_courant, map); break;
                case (gamestate.DEFAUT): gs = Menu.afficher_menu();
                    map.éteint();
                    map.retireCadavres(); break;
            }
            base.Update(gameTime);
            KeyboardState KState = Keyboard.GetState();
            if (KState.IsKeyDown(Keys.Escape))
            { this.Exit(); }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Azure);
            MouseState MState = Mouse.GetState();
            curseurp = new Vector2(MState.X, MState.Y);
            spriteBatch.Begin();
            spriteBatch.Draw(nuite, nuitepos, Color.White);

            if (menu == 0 && fonctions.déplacé == false && fonctions.a_agi == false)
            {
                if (MState.X >= 90 && MState.X <= 250 && MState.Y >= 310 && MState.Y <= 330)
                {

                    spriteBatch.Draw(parchemint7, parcheminPosition, Color.White);
                }
                else if (MState.X >= 90 && MState.X <= 250 && MState.Y >= 255 && MState.Y <= 275)
                {

                    spriteBatch.Draw(parchemint6, parcheminPosition, Color.White);
                }
                else if (MState.X >= 90 && MState.X <= 250 && MState.Y >= 360 && MState.Y <= 380)
                {

                    spriteBatch.Draw(parchemint9, parcheminPosition, Color.White);
                }

                else
                {
                    spriteBatch.Draw(parcheminTexture, parcheminPosition, Color.White);
                }
            }

            else if (menu == 0 && fonctions.a_agi == true && fonctions.déplacé == true)
            {
                if (MState.X >= 90 && MState.X <= 250 && MState.Y >= 360 && MState.Y <= 380)
                {
                    spriteBatch.Draw(parchemint12, parcheminPosition, Color.White);
                }
                else
                {
                    spriteBatch.Draw(parchemint5, parcheminPosition, Color.White);
                }
                
            }
            else if (menu == 0 && fonctions.a_agi == true)
            {
                if (MState.Y >= 360 && MState.Y <= 380 && Game1.menu == 0 && MState.X >= 90 && MState.X <= 250)
                {
                    spriteBatch.Draw(parchemint11, parcheminPosition, Color.White);
                }
                else if (MState.Y >= 310 && MState.Y <= 330 && Game1.menu == 0 && MState.X >= 90 && MState.X <= 250)
                {
                    spriteBatch.Draw(parchemint13, parcheminPosition, Color.White);
                }
                else
                {
                    spriteBatch.Draw(parchemint4, parcheminPosition, Color.White);
                }

            }
            else if (menu == 0 && fonctions.déplacé == true)
            {
                if (MState.X >= 90 && MState.X <= 250 && MState.Y >= 255 && MState.Y <= 275)
                {
                    spriteBatch.Draw(parchemint14, parcheminPosition, Color.White);
                }
                else if (MState.X >= 90 && MState.X <= 250 && MState.Y >= 360 && MState.Y <= 380)
                {
                    spriteBatch.Draw(parchemint15, parcheminPosition, Color.White);
                }
                else
                {
                    spriteBatch.Draw(parchemint3, parcheminPosition, Color.White);
                }

            }



            else if (menu != 0 && fonctions.a_agi == false)

            {
                if (MState.X >= 90 && MState.X <= 250 && MState.Y > 275 && MState.Y <= 295) //Attaque
                {
                    spriteBatch.Draw(parchemint10, parcheminPosition, Color.White);
                }
                else   if (MState.X >= 90 && MState.X <= 250 && MState.Y > 330 && MState.Y <= 345  ) //Retour
                {
                             spriteBatch.Draw(parchemint8, parcheminPosition, Color.White);
                }
                else 
                {
                spriteBatch.Draw(parchemint2, parcheminPosition, Color.White);
                }

            }

    
        
            spriteBatch.End();
            // TODO: Add your drawing code here
            map.Draw(spriteBatch);
            spriteBatch.Begin();
            spriteBatch.Draw(curseurText, curseurp, Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
